package com.wjv.example.spbt.mybatis.mapper;

import com.wjv.example.spbt.mybatis.entity.DictGenreEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

/**
 * <p>Title: DictGenreMapper</p>
 *
 * @author wjv
 * @date 2021/4/9 17:45
 * @description
 */
@Mapper
public interface DictGenreMapper {

    @InsertProvider(type = DictGenreSqlProvider.class)
    /** 方式一 返回 主键自增 id */
    // @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    /** 方式二 返回 主键自增 id */
    @SelectKey(statement = "select last_insert_id()", before = false, keyColumn = "id", keyProperty = "entity.id", resultType = Integer.class)
    Integer save(@Param("entity") DictGenreEntity entity);

    class DictGenreSqlProvider implements ProviderMethodResolver {
        public static String save() {
            SQL sql = new SQL() {{
                INSERT_INTO("dict_genre");
                INTO_COLUMNS("genre_name");
                INTO_VALUES("#{entity.genreName}");
            }};

            return sql.toString();
        }
    }
}
